package top.hymsk.ecommerce.controller.restful;

import org.springframework.web.bind.annotation.*;
import top.hymsk.ecommerce.anno.Log;
import top.hymsk.ecommerce.anno.TokenAnalysis;
import top.hymsk.ecommerce.pojo.response.GeneralResponse;
import top.hymsk.ecommerce.pojo.status.ResponseStatusCode;
import top.hymsk.ecommerce.service.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户模块 /user
 * (基本完成)
 *
 * @author 花于陌上开
 */
@CrossOrigin  //允许跨域访问
@RestController
@RequestMapping("/user")
public class UserRestController {
    final UserService userService;
    private final HttpServletRequest request;

    public UserRestController(UserService userService, HttpServletRequest request) {
        this.userService = userService;
        this.request = request;
    }

    /**
     * 查询用户名是否存在
     *
     * @param username 用户名
     * @return 返回状态信息
     */
    @Log
    @GetMapping("/status/exist/")
    public GeneralResponse userNameIsExist(String username) {
        return userService.userNameIsExist(username);
    }

    /**
     * 获取图灵测试验证码
     *
     * @param parameter 手机号/邮箱
     * @return 返回状态信息及图片信息
     */
    @Log
    @PostMapping("/verification/turing/")
    public GeneralResponse getVerificationTuring(String parameter) {
        if (parameter.length() == 0) {
            return GeneralResponse.fail(ResponseStatusCode.PARAMETER_INPUT_ERROR);
        }
        return userService.getVerificationTuring(parameter);
    }

    /**
     * 获取验证码
     *
     * @param parameter        手机号/邮箱
     * @param verificationCode 图灵测试验证码
     * @return 返回状态信息
     */
    @Log
    @PostMapping("/verification/message/")
    public GeneralResponse sendVerificationCode(String parameter, String verificationCode) {
        return userService.sendVerificationCode(parameter, verificationCode);
    }

    /**
     * 进行注册
     *
     * @param username         用户名
     * @param parameter        手机号/邮箱
     * @param verificationCode 验证码
     * @param password         密码
     * @return 返回状态信息
     */
    @Log
    @PostMapping("/register/")
    public GeneralResponse userRegister(String username, String parameter, String verificationCode, String password) {
        return userService.userRegister(username, parameter, verificationCode, password);
    }

    /**
     * 重置密码
     *
     * @param parameter        手机号/邮箱
     * @param verificationCode 验证码
     * @param password         密码
     * @return 返回状态信息
     */
    @Log
    @PostMapping("/register/reset/")
    public GeneralResponse userResetPassword(String parameter, String verificationCode, String password) {
        return userService.userResetPassword(parameter, verificationCode, password);
    }

    /**
     * 登录
     *
     * @param parameter 用户名/手机号/邮箱(自动匹配)
     * @param password  密码
     * @return 返回状态信息
     */
    @Log
    @PostMapping("/login/")
    public GeneralResponse login(String parameter, String password) {
        return userService.login(parameter, password);
    }

    /**
     * 注册为商户
     *
     * @param token     用户鉴权token
     * @param storeName 店铺名
     * @return 返回状态信息，需要重新登录才能生效
     */
    @Log
    @PostMapping("/join/seller")
    @TokenAnalysis
    public GeneralResponse joinSeller(String token, String storeName) {
        Integer userId = (Integer) request.getAttribute("userId");
        Integer storeId = (Integer) request.getAttribute("storeId");
        if (storeId != null) {
            return GeneralResponse.fail(ResponseStatusCode.USER_HAS_STORE);
        }
        return userService.joinSeller(userId, storeName);
    }

    /**
     * 获取店铺信息
     *
     * @param token     用户鉴权token
     * @return 返回状态信息，需要重新登录才能生效
     */
    @Log
    @PostMapping("/seller/info")
    @TokenAnalysis
    public GeneralResponse sellerInfo(String token) {
        Integer userId = (Integer) request.getAttribute("userId");
        Integer storeId = (Integer) request.getAttribute("storeId");
        if (storeId== null) {
            return GeneralResponse.fail(ResponseStatusCode.PERMISSION_ERROR);
        }
        return userService.sellerInfo(userId,storeId);
    }

    /**
     * 获取用户信息
     *
     * @param token     用户鉴权token
     * @return 返回状态信息，需要重新登录才能生效
     */
    @Log
    @PostMapping("/buyer/info")
    @TokenAnalysis
    public GeneralResponse buyerInfo(String token) {
        Integer userId = (Integer) request.getAttribute("userId");
        return userService.buyerInfo(userId);
    }
}
